package com.loby.controller;
import com.loby.data.base.pojo.RtMonStationPollutant;
import com.loby.data.util.StationParam;
import com.loby.data.util.TimeDataParam;
import lombok.extern.slf4j.Slf4j;

import com.loby.data.base.pojo.TBaseOrganization;
import com.loby.data.base.pojo.TMonMonitorStation;
import com.loby.data.mapper.AdministrativeDivisionMapper;
import com.loby.data.rest.RestResult;
import com.loby.recordkey.injecter.annotations.RecordKeysString;
import com.loby.recordkey.injecter.commons.KeyType;
import com.loby.service.BaseMonitorDataServiceApiImpl;
import com.loby.data.base.pojo.TMonSourceShops;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 操作基础信息，插入排放口等
 */

// 127.0.0.1:9090/test/baseMonitorData/tree
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/baseMonitor")
public class BaseMonitorDataController  {
    private String realType="real";
    private String minType="min";
    private String hourType="hour";
    private String dayType="day";

    @Autowired
    private BaseMonitorDataServiceApiImpl baseMonitorDataServiceApi;

    @Autowired
    AdministrativeDivisionMapper divisionMapper;

    @ApiOperation(value="更新zookeeper排放口数据API",notes = "第一个测试api",response = RestResult.class,
            httpMethod = "GET",produces = "json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "排放口id", required = true, paramType = "path", dataType = "long",
                    example = "22")})
    @RequestMapping("/update/station/{id}")
    public RestResult updateByMonitorStationId(@PathVariable("id") long id) throws Exception{
        System.out.println(id);
        return baseMonitorDataServiceApi.updateByMonitorStationId(id);
    }

    @ApiIgnore
    @RequestMapping("/org")
    public RestResult newOrganization(TBaseOrganization tBaseOrganization)  {

        System.out.println(tBaseOrganization);
        return  baseMonitorDataServiceApi.newOrganization(tBaseOrganization);
    }
    @ApiIgnore
    @GetMapping("/org/{id}")
    public RestResult removeOrganization(@PathVariable("id") long id)
    {
        System.out.println(id);
       return  baseMonitorDataServiceApi.removeOrganization(id);
    }
    @ApiIgnore
    @GetMapping("/org/update")
    public RestResult updateOrganization(TBaseOrganization organization)
    {

        return  baseMonitorDataServiceApi.updateOrganization(organization);
    }
    @ApiIgnore
    @RequestMapping("/shop")
    public RestResult newShop(TMonSourceShops monSourceShops) throws Exception{

        return baseMonitorDataServiceApi.newShop(monSourceShops);
    }
    @ApiIgnore
    @GetMapping("/shop/{id}")
    public RestResult removeShop(@PathVariable("id") long id){
        System.out.println(id);
        return baseMonitorDataServiceApi.removeShop(id);
    }
    @ApiIgnore
    @PutMapping("/shop/update")
    public RestResult updateShops(TMonSourceShops monSourceShops){
        return baseMonitorDataServiceApi.updateShop(monSourceShops);
    }
    @ApiIgnore
    @RequestMapping("/monstation")
    public RestResult insertMonitorStation(TMonMonitorStation monitorStation){
        System.out.println(monitorStation);
        return baseMonitorDataServiceApi.newMonitorStation(monitorStation);
    }

    @ApiIgnore
    @GetMapping("/monstation/{id}")
    public RestResult removeMonitorStation(@PathVariable("id") long id){
        return baseMonitorDataServiceApi.removeMonitorStation(id);
    }

    @GetMapping("/tree/{typeId}")
    public RestResult getMonitorStationTree(@PathVariable("typeId") int typeId,@RecordKeysString(type = "STATION", keyType = KeyType.Long)String keys){
        log.info(keys);
        return baseMonitorDataServiceApi.getStationTree(typeId,keys);
    }

    @RequestMapping("/monstation/poll")
    public RestResult insertRtStationPollutant(RtMonStationPollutant pollutant){
      return   baseMonitorDataServiceApi.newRtStationPollutant(pollutant);
    }

    @PostMapping("/getByCode")
    public RestResult getMultipulStationByCode(StationParam param, @RecordKeysString(type = "STATION", keyType = KeyType.Long)String keys){

        return baseMonitorDataServiceApi.getMultipulStationByCode(param.getCode(),param.getTypeId(),keys);
    }
    @RequestMapping("/getByCodes")
    public RestResult getMultipulStationsByMNCodes(@RequestParam(value = "code",required = false) String[] codes, @RecordKeysString(type = "STATION", keyType = KeyType.Long)String keys){

        return baseMonitorDataServiceApi.getMultipulStationsByMNCodes(codes,keys);
    }
    @GetMapping("/getById/{id}")
    public RestResult getChildStationsByAdCode(@PathVariable(value = "id")Long id){
        return baseMonitorDataServiceApi.getStationById(id);
    }

    @GetMapping("/getAll/{typeId}")
    public RestResult getAllOrgStations(@PathVariable("typeId") int typeId,@RecordKeysString(type = "STATION", keyType = KeyType.Long)String keys){
        return baseMonitorDataServiceApi.getAllStations(typeId,keys);
    }


    @PostMapping("/getDatasByTime")
    public RestResult getDatasByTime(TimeDataParam param){
        if(!ObjectUtils.isEmpty(param.getMnCode())){
            if (ObjectUtils.isEmpty(param.getTimeType())||ObjectUtils.isEmpty(param.getStartTime())||ObjectUtils.isEmpty(param.getEndTime())){
                return baseMonitorDataServiceApi.getAllTimeDatas(param.getMnCode());
            }
            switch (param.getTimeType()){
                case "min":
                    return baseMonitorDataServiceApi.getMinDatas(param);
                case "hour":
                    return baseMonitorDataServiceApi.getHourDatas(param);
                case "day":
                    return baseMonitorDataServiceApi.getDayDatas(param);
                default:return RestResult.successMessageOf("无数据");
            }
        }else {
            return RestResult.successMessageOf("请选择排放口!");
        }
    }

    @PostMapping("/getMinDatas")
    public RestResult getMinDatas(TimeDataParam param){
        return baseMonitorDataServiceApi.getMinDatas(param);
    }

    @PostMapping("/getHourDatas")
    public RestResult getHourDatas(TimeDataParam param){
        return baseMonitorDataServiceApi.getHourDatas(param);
    }

    @PostMapping("/getDayDatas")
    public RestResult getDayDatas(TimeDataParam param){
        return baseMonitorDataServiceApi.getDayDatas(param);
    }

    @GetMapping("/getAllTimeDatas/{code}")
    public RestResult getAllTimeDatas(@PathVariable("code") String mnCode){
        return baseMonitorDataServiceApi.getAllTimeDatas(mnCode);
    }







}
